const Service = require('egg').Service

class ConfigService extends Service {
  // 根据type获取配置
  async getConfigByType(type) {
    const { ctx } = this

    const doc = await ctx.model.Config.findOne({
      configType: type.replace('-', '_'),
      isOpen: true,
    })

    if (!doc) throw '无效配置'

    return {
      configId: doc.id,
      ...doc.config,
    }
  }

  async getConfig(configType) {
    const { ctx } = this

    const doc = await ctx.model.Config.findOne({
      configType,
    })

    return {
      ...doc.config,
    }
  }
}

module.exports = ConfigService
